package com.sita.bike.ui.adapter;

import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.sita.bike.R;
import com.sita.bike.support.GlobalContext;
import com.sita.bike.ui.activity.DataAnalysisActivity;

import java.util.List;

/**
 * Created by lijuan zhang on 2016/3/4.
 */
public class DayGatherAdapter extends RecyclerView.Adapter<DayGatherAdapter.ViewHolder> {

    public List<String> mDataset;
    public int recyclerViewWidth;
    private int selectItem;
    private OnItemClickListener mOnItemClickListener = null;

    public DayGatherAdapter(List<String> dataset, int width) {
        super();
        mDataset = dataset;
        recyclerViewWidth = width;
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.mOnItemClickListener = listener;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
        View view = View.inflate(viewGroup.getContext(), R.layout.month_list_item, null);
        ViewHolder holder = new ViewHolder(view);
        return holder;
    }


    @Override
    public void onBindViewHolder(final ViewHolder holder, final int position) {
        LinearLayout.LayoutParams linearParams = (LinearLayout.LayoutParams) holder.mTextView.getLayoutParams();
        // 取控件aaa当前的布局参数
        linearParams.width = recyclerViewWidth / 7; // 当控件的高强制设成365象素
        holder.mTextView.setLayoutParams(linearParams); // 使设置好的布局参数应用到控件aaa
        holder.mTextView.setText(mDataset.get(position).toString());
        holder.setIsRecyclable(false);
        if (String.valueOf(DataAnalysisActivity.getSystemDay()).equals(mDataset.get(position))) {
            DataAnalysisActivity.getInstance().setCurrentDay(Integer.valueOf(holder.mTextView.getText().toString()));
        }

        if (position < 3 || position > mDataset.size() - 4) {
            holder.mImageSign.setBackgroundColor(GlobalContext.getGlobalContext().getResources().getColor(R.color.data_analysis_background));
        } else {
            if (position == selectItem) {
                holder.mImageSign.setBackground(GlobalContext.getGlobalContext().getResources().getDrawable(R.drawable.btn_bg_selected));
            } else {
                holder.mImageSign.setBackground(GlobalContext.getGlobalContext().getResources().getDrawable(R.drawable.btn_bg_unselected));
            }
        }

        if (mOnItemClickListener != null) {
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mOnItemClickListener.onItemClick(holder.itemView, position);
                }
            });
        }

    }


    public void setSelectItem(int selectItem) {
        this.selectItem = selectItem;
    }

    @Override
    public int getItemCount() {
        return mDataset.size();
    }

    public interface OnItemClickListener {
        void onItemClick(View view, int position);
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        public LinearLayout mMonthListItem;
        public TextView mTextView;
        public ImageView mImageSign;

        public ViewHolder(View itemView) {
            super(itemView);
            mTextView = (TextView) itemView.findViewById(R.id.tv_date);
            mImageSign = (ImageView) itemView.findViewById(R.id.sign_date);
            mMonthListItem = (LinearLayout) itemView.findViewById(R.id.month_list_item);
        }
    }
}
